package com.lanou.dllo.vc.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.adapter.GroupAdapter;
import com.lanou.dllo.vc.entity.GroupUserName;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FriendsListActivity extends BaseActivity implements View.OnClickListener {
    private List<String> usernames;
    private ListView friends_list;
    private GroupAdapter adapter;
    private String[] members;
    private TextView listSave;
    private String groupName ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_friends_list;
    }

    @Override
    protected void initView() {
        usernames = new ArrayList<>();
        friends_list = (ListView) findViewById(R.id.friends_list);
        listSave = (TextView) findViewById(R.id.listSave);

        adapter = new GroupAdapter(this);
        friends_list.setAdapter(adapter);
        Log.i("lanougroup", members + "订单");
        listSave.setOnClickListener(this);

        Intent intent = getIntent();
        if (intent!=null) {
            groupName = intent.getStringExtra("groupId");
            Log.i("lanou","listgroup------->"+groupName);
        }


    }

    @Override
    protected void initData() {
        EMChat.getInstance().setAppInited();
        //如果使用环信的好友体系需要先设置
        EMChatManager.getInstance().getChatOptions().setUseRoster(true);

        new getFriendThred().start();


        }

    @Override
    public void onClick(View v) {

     List<GroupUserName> gr = adapter.getCheckedName();
        members = new String[gr.size()];
        Log.i("lanou",gr+"------");
        for (int i = 0; i < gr.size(); i++) {
            String username = gr.get(i).getUserName();
           // members.add(username);
            members[i] = username;

        }

        Log.i("lanou888", members+"");


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMGroupManager.getInstance().addUsersToGroup(groupName,members);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        Intent intent = new Intent();
        intent.putExtra("members",members);
        setResult(RESULT_OK,intent);
        finish();

    }



    class getFriendThred extends Thread {
        @Override
        public void run() {
            super.run();

            try {
                usernames = EMContactManager.getInstance().getContactUserNames();//需异步执行

                Log.i("lanoufriend", usernames.size() + "--->>");

                adapter.addData(usernames);


            } catch (EaseMobException e) {
                e.printStackTrace();
            }
        }
    }



}
